<?php
/**
 * @package     system
 * @subpackage  jsCall
 */
/**
 * A command that composes a JSCall
 * @see JsCall
 */
class JsCommand {
	
  private $controller;
  private $action;
  private $parent;
  private $arParam;
  
  function toJson() {
  	$json =  
  	  '  {"controller":"'.$this->controller.'",'."\n".
  	  '   "action":"'.$this->action.'"'."\n";
  	$json .= $this->paramsToJson();  
    $json .= '  }'."\n";
    return $json;
  }
  
  function paramsToJson() {
  	$json = '';
  	if(count($this->arParam)) {
  	  $json .= ',';
      $json .= '   "params":{'."\n";
      $comma = false;
      foreach($this->arParam as $name=>$param) {
  	    if($comma) {
		  $json .= ",";
  		}
  		$json .= 
  		  '     "'.$name.'":'.$param."\n";
  		$comma = true;
  	  }
  	  $json .= '   }'."\n";
  	}//if
  	return $json;
  }
  
  function __construct($controller,$action,$parent) {
  	$this->controller = $controller;
  	$this->action = $action;
  	$this->parent = $parent;
  }

  function addJsonParam($name,$param) {
    $this->arParam[$name] = $param;
    return $this;	
  }
  
  function addPHPParam($name,$param) {
    $this->arParam[$name] = json_encode($param);
  }
  
}
?>